package org.ax.service.device.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.device.DeviceMapper;
import org.ax.pojo.device.Device;
import org.ax.service.device.DeviceService;
import org.ax.service.device.DeviceTypeService;
import org.ax.service.employ.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-28-23:27
 */
@Service
public class IDeviceService extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DeviceTypeService deviceTypeService;
    @Override
    public Page<Device> searchDevices(Page page, String deviceTypeId) {
        QueryWrapper<Device> queryWrapper=new QueryWrapper<Device>();
        queryWrapper.eq(!StringUtils.isBlank(deviceTypeId),"device_type_id",deviceTypeId);
        Page page1 = getBaseMapper().selectPage(page, queryWrapper);
        List<Device> devices=(List<Device>)page1.getRecords();
        for (Device device : devices) {
            device.setDeviceType(deviceTypeService.toDeviceTypeId(device.getDeviceTypeId()));
            device.setEmployee(employeeService.toEmployeeId(device.getDeviceKeeperId()));
        }
        page1.setRecords(devices);
        return page1;
    }

    @Override
    public boolean addDevice(Device device) {
        return getBaseMapper().insert(device)>0;
    }

    @Override
    public boolean updateDevice(Device device) {
        return getBaseMapper().updateById(device)>0;
    }

    @Override
    public boolean deletesDevice(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public Device toDeviceId(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<Device> findDevices() {
        return getBaseMapper().selectList(null);
    }
}
